package com.huizluo.module4.student;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


public class ListStore implements Store {

    private String dbPath = "/tmp/s.db";
    private List<Item> db = new ArrayList<Item>();
    private HashMap<Integer,Integer> index = new HashMap<>();

    {
        File f = new File(this.dbPath);
        if (!f.isFile()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean Add(Item item) throws Exception {
        if (this.index.get(item.ID) !=null){
            throw new Exception("same ID " + item.ID + " exist");
        }
        this.db.add(item);
        this.index.put(item.ID,this.db.indexOf(item));
        return true;
    }

    @Override
    public Item Find(Integer Id) throws Exception {
        Integer index = this.index.get(Id);
        if (index == null){
            throw new Exception("ID " + Id + " can not found");
        }
        return this.db.get(index);
    }

    @Override
    public boolean Remove(Item t) throws Exception {
        Integer index = this.index.get(t.ID);
        if (index == null){
            throw new Exception("ID " + t.ID + " can not found");
        }

        this.db.remove(index.intValue());
        this.index.remove(t.ID);
        return true;
    }

    @Override
    public boolean Update(Item t) throws Exception {
        Integer index = this.index.get(t.ID);
        if (index == null){
            throw new Exception("ID " + t.ID + " can not found");
        }
        this.db.set(index,t);
        return true;
    }

    @Override
    public Item[] List() {
        int size = this.db.size();
        Item[] items = new Item[size];
        for(int i=0;i<this.db.size();i++){
            items[i] = this.db.get(i);
        }
        return items;
    }

    boolean SaveDB(Item[] items) throws IOException {
        FileOutputStream fo = new FileOutputStream(this.dbPath);
        ObjectOutputStream os = new ObjectOutputStream(fo);

        os.writeObject(items);
        return true;
    }

    Item[] LoadDB() throws IOException, ClassNotFoundException {
        FileInputStream fi = new FileInputStream(this.dbPath);
        ObjectInputStream ois = new ObjectInputStream(fi);

        Item[] items = (Item[])ois.readObject();
        ois.close();
        return items;
    }

    public boolean Save() {
        Item[] items  = this.List();
        try {
            this.SaveDB(items);
        }catch (IOException e){
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }

    public boolean Load() {
        try {
            Item[] items = this.LoadDB();
            this.db.addAll(Arrays.asList(items));
        }catch (IOException|ClassNotFoundException e){
            System.out.println(e.getMessage());
            return false;
        }
        return true;
    }


}
